#!/bin/bash

clear
if [ $# -ne 2 ];then
	echo "Usage: $0 projectname pkglist"
	exit 1
fi

echo "=============================================Begin============================================="
# get succeed pkglist
rm -fr /srv/lao_wang >/dev/null
#rm -f pkg_rlt succ_lt
mkdir /srv/lao_wang
cp check_pkg_install_in_docker.sh $2 /srv/lao_wang
cd /srv/lao_wang
#osc r --csv $1 --repo=standard_aarch64 --arch=aarch64 &>/dev/null > pkg_rlt
#cat pkg_rlt | grep "succeeded" | awk -F ';' '{print $1}' > succ_lt
#if [[ ! -s succ_lt ]];then
#	echo "$1 do not has status is succeed!"
#	exit 2
#fi

# download binaries file
echo "download binary file is doing, please wait..."
for name in `cat $2`
do	
	osc getbinaries $1 $name standard_aarch64 aarch64 &>/dev/null >/dev/null
	#osc getbinaries $1 $name standard_x86_64 x86_64
	if [ $? -ne 0 ];then
		echo "download $name binary file failed!"
		exit 3
#	else
#		echo "download $name binary file succeed!"
	fi
done
sleep 1
rm -f succ_lt pkg_rlt
echo "download all binary files have done!"

# create docker env
echo "creating docker env, please wait..."
yum install axel -y >/dev/null
axel -n 10 http://117.78.1.88:82/dailybuilds/openeuler/mainline/openeuler_ARM64/openeuler-2020-08-01-15-06-53/Software/aarch64/DockerStack/openEuler-docker.aarch64.tar.xz >/dev/null
docker load < openEuler-docker.aarch64.tar.xz >/dev/null
rm openEuler-docker.aarch64.tar.xz -f
docker stop test_install &>/dev/null
docker rm test_install &>/dev/null
docker run -itd --name=test_install openeuler-20.03-lts:latest /bin/bash >/dev/null
docker cp /srv/lao_wang test_install:/root/
echo "create docker env succeed!"
docker exec -it test_install /bin/bash -c 'cd /root/lao_wang;bash check_pkg_install_in_docker.sh'
docker cp test_install:/root/lao_wang/check_results ./
rm check_pkg_install_in_docker.sh -f
#docker stop test_install >/dev/null
#docker rm test_install >/dev/null
echo "checking install rely succeed, you can see log file in directory: /srv/lao_wang/check_results/"
echo "=============================================End============================================="
